package com.bwie.util;

import cn.hutool.extra.qrcode.QrConfig;

import java.awt.*;
import java.awt.image.BufferedImage;
import cn.hutool.extra.qrcode.QrConfig;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.awt.*;
import java.awt.image.BufferedImage;
/**
 * @className: CreateCodeUtil
 * @Description: TODO
 * @version: v1.８.0
 * @author: 刘茂锴
 * @date: 2025/10/3 10:47
 */
@Component
public class CreateCodeUtil {


        //模板宽度
        public  final int modelImgWidth = 150;
        //模板高度
        public  final int modelHeight = 165;

        //二维码宽度
        public  final int codeWidth = 150;
        //二维码高度
        public  final int codeHeight = 150;

        //模块间距
        public  final int distance = 0;

        //文字大小
        public final int FontSize = 15;


        /**
         * 生成二维码--返回BufferedImage
         * @param context       二维码内容
         * @param titleHeader   头部文字
         * @param titleFooter   底部文字
         * @param imgLogoPath   logo路径
         * @return
         */
        public BufferedImage encode(String context, String titleHeader, String titleFooter, String imgLogoPath) {

            // 创建主模板图片
            BufferedImage image = new BufferedImage(modelImgWidth, modelHeight, BufferedImage.TYPE_INT_RGB);
            Graphics2D graphics2DModel = image.createGraphics();
            // 设置图片的背景色
            graphics2DModel.setColor(Color.white);
            graphics2DModel.fillRect(0, 0, modelImgWidth, modelHeight);

            //描绘的y轴
            int y = 0;

            //头部文字
            y = getY(titleHeader, image, y, true);

            //中间二维码
            if (StringUtils.isNotBlank(context)) {
                Graphics2D graphics2DCode = image.createGraphics();

                //hutool的QrConfig配置
                QrConfig qrConfig = new QrConfig();
                qrConfig.setHeight(codeWidth);
                qrConfig.setWidth(codeHeight);
                qrConfig.setBackColor(Color.WHITE);
                qrConfig.setErrorCorrection(ErrorCorrectionLevel.H);
                if (StringUtils.isNotBlank(imgLogoPath)) {
                    qrConfig.setImg(imgLogoPath);
                }
                BufferedImage bufferedImage = cn.hutool.extra.qrcode.QrCodeUtil.generate(context, qrConfig);
                int x = (modelImgWidth - qrConfig.getWidth()) / 2;
                graphics2DCode.drawImage(bufferedImage, x, y, qrConfig.getWidth(), qrConfig.getHeight(), null);
                //间距20
                y +=qrConfig.getHeight()+distance;
            }


            //尾部字体
            y = getY(titleFooter, image, y, false);

            return image;
        }

        /**
         * 获取y轴高度
         *
         * @param title
         * @param image
         * @param y
         * @param checkAdd
         * @return
         */
        public int getY(String title, BufferedImage image, int y, boolean checkAdd) {
            if (StringUtils.isNotBlank(title)) {
                //获取y轴(类似光标所在处)
                // y += FontSize;
                y += 0;
                Graphics2D graphics2DHead = image.createGraphics();
                //设置字体
                graphics2DHead.setColor(Color.black);
                Font font = new Font("宋体", Font.PLAIN, FontSize);
                graphics2DHead.setFont(font);
                //文字生成--抗锯齿
                graphics2DHead.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_DEFAULT);

                //文字长度--获取该文字的在该模板的总长度
                int textLenght = graphics2DHead.getFontMetrics().stringWidth(title);
                //居中=(模板总长度-文字长度)/2
                int x = (modelImgWidth - textLenght) / 2;
                graphics2DHead.drawString(title, x, y);
                //间距+20
                if (checkAdd) {
                    y += distance;
                }
            }
            return y;
        }



}
